/**
 * 缺陷状态 composable
 * 从状态管理模块获取缺陷状态选项
 */
import { computed } from 'vue'
import { useStateStore } from '@/stores/state'
import { ModuleType } from '@/types/state'

export function useDefectStates() {
  const stateStore = useStateStore()

  // 获取缺陷状态列表
  const defectStates = computed(() => {
    return stateStore.getStatesByModule(ModuleType.DEFECT)
  })

  // 获取启用的缺陷状态
  const enabledStates = computed(() => {
    return defectStates.value.filter(state => state.is_enabled)
  })

  // 获取可见的缺陷状态
  const visibleStates = computed(() => {
    return defectStates.value.filter(state => state.is_visible)
  })

  // 获取默认状态
  const defaultState = computed(() => {
    return defectStates.value.find(state => state.is_default) || defectStates.value[0]
  })

  // 获取状态选项（用于下拉框）- 只返回启用且可见的状态
  const statusOptions = computed(() => {
    return enabledStates.value.filter(state => state.is_visible).map(state => ({
      value: state.code,
      label: state.name,
      color: state.color,
      icon: state.icon,
      disabled: false  // 因为是启用的状态，所以都不禁用
    }))
  })

  // 根据代码获取状态
  const getStateByCode = (code: string) => {
    return defectStates.value.find(state => state.code === code)
  }

  // 根据代码获取状态名称
  const getStateName = (code: string) => {
    const state = getStateByCode(code)
    return state?.name || code
  }

  // 根据代码获取状态颜色
  const getStateColor = (code: string) => {
    const state = getStateByCode(code)
    return state?.color || '#909399'
  }

  // 根据代码获取状态类型（用于 Element Plus tag type）
  const getStatusType = (code: string): 'info' | 'success' | 'warning' | 'danger' | 'primary' => {
    const state = getStateByCode(code)
    if (state && state.color) {
      // 将颜色转换为 Element Plus 的 tag type
      const colorMap: Record<string, 'info' | 'success' | 'warning' | 'danger' | 'primary'> = {
        '#67C23A': 'success',
        '#E6A23C': 'warning',
        '#F56C6C': 'danger',
        '#409EFF': 'primary',
        '#909399': 'info'
      }
      // 尝试匹配颜色
      for (const [color, type] of Object.entries(colorMap)) {
        if (state.color.toLowerCase() === color.toLowerCase()) {
          return type
        }
      }
      // 如果颜色不匹配，根据状态名称判断
      if (state.name.includes('完成') || state.name.includes('已') || state.name.includes('成功') || state.name.includes('解决') || state.name.includes('验证')) {
        return 'success'
      }
      if (state.name.includes('进行') || state.name.includes('处理') || state.name.includes('中') || state.name.includes('分配')) {
        return 'warning'
      }
      if (state.name.includes('取消') || state.name.includes('失败') || state.name.includes('拒绝') || state.name.includes('重新打开')) {
        return 'danger'
      }
      if (state.name.includes('待') || state.name.includes('新')) {
        return 'info'
      }
    }
    // 如果没有匹配到，返回默认类型
    return 'info'
  }

  // getStatusName 作为 getStateName 的别名
  const getStatusName = getStateName

  // 检查状态代码是否有效
  const isValidState = (code: string) => {
    return enabledStates.value.some(state => state.code === code)
  }

  // 加载状态数据
  const loadStates = async () => {
    await stateStore.fetchModuleStates(ModuleType.DEFECT)
  }

  return {
    defectStates,
    enabledStates,
    visibleStates,
    defaultState,
    statusOptions,
    getStateByCode,
    getStateName,
    getStateColor,
    getStatusName,
    getStatusType,
    isValidState,
    loadStates
  }
}

